package com.example.smartcity_ikun3.logic

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.smartcity_ikun3.logic.model.BannerModel
import com.example.smartcity_ikun3.logic.network.RetrofitService.Companion.create
import com.example.smartcity_ikun3.logic.network.SmartApi
import com.example.smartcity_ikun3.logic.util.Util.show
import kotlinx.coroutines.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import androidx.lifecycle.viewModelScope
import com.example.smartcity_ikun3.logic.model.GetNewsUserModel
import com.example.smartcity_ikun3.logic.model.MoreModel
import com.example.smartcity_ikun3.logic.model.NewsModel
import com.youth.banner.Banner

/**
 * 网络回调仓库
 */
object Repository {
    private val api = create<SmartApi>()

    private val BannerLiveData = MutableLiveData<BannerModel>()
    private val NewsLiveData = MutableLiveData<NewsModel>()
    private val GetNewsUser = MutableLiveData<GetNewsUserModel>()
    private val MoreLiveData = MutableLiveData<MoreModel>()


    // Banner
    fun getBanner() : LiveData<BannerModel>{
        api.getBanner().enqueue(object : Callback<BannerModel>{
            override fun onFailure(p0: Call<BannerModel>, p1: Throwable) {
                "加载网络请求失败！".show()
                Log.d("NET", "onFailure: banner ---> ${p1.message}")
            }

            override fun onResponse(p0: Call<BannerModel>, p1: Response<BannerModel>) {
                val body = p1.body()
                if (body != null) if (body.code == "200"){
                    BannerLiveData.value = body
                }
            }

        })
        return BannerLiveData
    }



    /**
     * News
     */
    fun getNews(type: Int?, id: Int?, hot: String?) : LiveData<NewsModel>{
        api.getNews(type, id, hot).enqueue(object : Callback<NewsModel>{
            override fun onFailure(p0: Call<NewsModel>, p1: Throwable) {
                "加载新闻失败！".show()
                Log.d("NET", "onFailure: News ---> ${p1.message}")
            }

            override fun onResponse(p0: Call<NewsModel>, p1: Response<NewsModel>) {
                val body = p1.body()
                if (body != null) if (body.code == 200){
                    NewsLiveData.value = body
                }else "加载新闻出错".show()
            }

        })
        return NewsLiveData
    }

    /**
     * 获取新闻评论列表
     */
    fun getNewsUser() : LiveData<GetNewsUserModel>{
        api.getNewsUser().enqueue(object : Callback<GetNewsUserModel>{
            override fun onFailure(p0: Call<GetNewsUserModel>, p1: Throwable) {
                "获取新闻评论列表失败！".show()
                Log.d("NET", "onFailure: getNewsUser --> ${p1.message}")
            }

            override fun onResponse(p0: Call<GetNewsUserModel>, p1: Response<GetNewsUserModel>) {
                val body = p1.body()
                if (body != null){
                    if (body.code == 200){
                        "哈哈哈".show()
                    }else "加载新闻评论失败！".show()
                }else "空空".show()
            }

        })

        return GetNewsUser
    }


    /**
     * 查询缴费分类
     */
    fun getMore() : LiveData<MoreModel>{
        api.getMore().enqueue(object : Callback<MoreModel>{
            override fun onFailure(p0: Call<MoreModel>, p1: Throwable) {
                "查询失败".show()
                Log.d("NET", "onFailure: ---> ${p1.message}")
            }

            override fun onResponse(p0: Call<MoreModel>, p1: Response<MoreModel>) {
                val body = p1.body()
                if (body != null){
                    if (body.code == 200){
                        MoreLiveData.value = body
                    }else "加载缴费服务失败".show()
                }else "空空".show()
            }
        })

        return MoreLiveData
    }

















}